// pages/my/index.js
var app = getApp();
const api = require('../../utils/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin:false,
        userInfo:{},
        collection:0,
        appointment:0,
        appointmentList:[],
        success:0,
        view:0,
        page:1,
        carList:[],
        buttonList:{
            bt1:false,
            bt2:false,
            bt3:true,
            bt4:true
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //判断登录
        
    },
    getList(){
        let obj = {
            page: this.data.page,
            pagesize: 10,
        }
        api.recommendlist({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    carList:res.data.data.data
                })
            }
        })
    },
    getCollection(){
        let obj = {
            opcode: "Int16Array", 
            clientid: wx.getStorageSync('id'), 
            page: 1, 
            pagesize: 100 
        }
        let that = this
        api.collectionlist({
            data:obj,
            method:'post',
            success: function(res){
                console.log(res.data.data.data)
                if(res.data.data.data){
                    that.setData({
                        collection:res.data.data.data.length
                    })
                }
            }
        })
    },
    getOrderList(id){
        let obj = {
            opcode: "Int16Array",
            clientid: id,
            status:1,
            page: 1,
            pagesize: 1000,
        }
        api.orderlist({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    appointment:res.data.data.data.length,
                    appointmentList: res.data.data.data
                })

            }
        })
        let obj2 = {
            opcode: "Int16Array",
            clientid: id,
            status:3,
            page: 1,
            pagesize: 1000,
        }
        api.orderlist({
            data:obj2,
            method:'post',
            success: (res)=>{
                if(res.data.data.data){
                    this.setData({
                        success: res.data.data.data.length || 0
                    })
                }
            }
        })
    },
    getViewList(){
        let data = wx.getStorageSync('viewList')
        this.setData({
            view:data.length
        })
    },
    handleApp(){
        wx.navigateTo({
          url: '/pages/appointment/index',
        })
    },
    handleView(){
        wx.navigateTo({
          url: '/pages/view/index',
        })
    },
    handleSuccess(){
        wx.navigateTo({
          url: '/pages/success/index',
        })
    },
    handleCol(){
        wx.navigateTo({
          url: '/pages/collection/index',
        })
    },
    handleSetting(){
        wx.navigateTo({
            url: '/pages/setting/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let phone =wx.getStorageSync('phone')
        let id = wx.getStorageSync('id')
        this.setData({
            userInfo: wx.getStorageSync('userInfo')
        })
        if(phone && id){
            this.setData({
                isLogin:true
            })
        }else{
            this.setData({
                isLogin:false
            })
        }
        
        if(this.data.isLogin){
            this.getCollection()
            this.getOrderList(id)
            this.getViewList()
        }else{
            this.setData({
                appointment:0,
                appointmentList:[],
                view:0,
                success:0,
                collection:0
            })
        }
        // 推荐
        this.getList()
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})